﻿using Admin.NET.Core.Service;
using Admin.NET.Application.Const;
using Admin.NET.Application.Entity;
using Microsoft.AspNetCore.Http;
using System.Collections.Generic;
using System.Threading.Tasks;
using Admin.NET.Application.Service.TopSdk;
namespace Admin.NET.Application;
/// <summary>
/// 校本选课服务
/// </summary>
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
public class MyXuankeService : IDynamicApiController, ITransient
{
    private readonly SqlSugarRepository<XuanKe> _rep;
    private readonly SqlSugarRepository<Kecheng> _kechengRep;
    private readonly UserManager _userManager;
    private readonly SqlSugarRepository<JiaoxueBan> _jiaoxueBanRep;
    private readonly SqlSugarRepository<Teacher> _teacherRep;
    private readonly TopSdkService _topSdkService;

    public MyXuankeService(SqlSugarRepository<XuanKe> rep, UserManager userManager, SqlSugarRepository<Kecheng> kechengRep,SqlSugarRepository<JiaoxueBan> jiaoxueBanRep,
        SqlSugarRepository<Teacher> teacherRep,
        TopSdkService topSdkService)
    {
        _rep = rep;
        _userManager = userManager;
        _kechengRep = kechengRep;
        _topSdkService = topSdkService;
        _jiaoxueBanRep = jiaoxueBanRep;
        _teacherRep = teacherRep;
    }

    /// <summary>
    /// 分页查询校本选课
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Page")]
    public async Task<SqlSugarPagedList<MyXuanKeOutput>> Page(XuanKeInput input)
    {
        var userId = _userManager.UserId;
        var query = _rep.AsQueryable()
            .WhereIF(!string.IsNullOrWhiteSpace(input.SearchKey), u =>
                u.Chengji.Contains(input.SearchKey.Trim())
                || u.Remark.Contains(input.SearchKey.Trim())
            )
            .WhereIF(input.XuankeTaskId>0, u => u.XuankeTaskId == input.XuankeTaskId)
            .WhereIF(input.BanjiId>0, u => u.BanjiId == input.BanjiId)
            .WhereIF(input.JiaoxueBanId>0, u => u.JiaoxueBanId == input.JiaoxueBanId)
            .WhereIF(input.StudentId>0, u => u.StudentId == input.StudentId)
            .WhereIF(input.KechengId>0, u => u.KechengId == input.KechengId)
            .WhereIF(!string.IsNullOrWhiteSpace(input.Chengji), u => u.Chengji.Contains(input.Chengji.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.Remark), u => u.Remark.Contains(input.Remark.Trim()))
            //处理外键和TreeSelector相关字段的连接
            .LeftJoin<XuankeTask>((u, xuanketaskid) => u.XuankeTaskId == xuanketaskid.Id )
            .LeftJoin<Banji>((u, xuanketaskid, banjiid) => u.BanjiId == banjiid.Id )
            .LeftJoin<JiaoxueBan>((u, xuanketaskid, banjiid, jiaoxuebanid) => u.JiaoxueBanId == jiaoxuebanid.Id )
            .LeftJoin<Student>((u, xuanketaskid, banjiid, jiaoxuebanid, studentid) => u.StudentId == studentid.Id )
            .LeftJoin<Kecheng>((u, xuanketaskid, banjiid, jiaoxuebanid, studentid, kechengid) => u.KechengId == kechengid.Id )
            .Where((u, xuanketaskid, banjiid, jiaoxuebanid, studentid, kechengid) => studentid.Id == userId && xuanketaskid.Active == true)
            .OrderBy(u => u.CreateTime)
            .Select((u, xuanketaskid, banjiid, jiaoxuebanid, studentid, kechengid) => new MyXuanKeOutput
            {
                Id = u.Id,
                XuankeTaskId = u.XuankeTaskId, 
                XuankeTaskIdName = xuanketaskid.Name,
                BanjiId = u.BanjiId, 
                BanjiIdName = banjiid.Name,
                JiaoxueBanId = u.JiaoxueBanId, 
                JiaoxueBanIdName = jiaoxuebanid.Name,
                StudentId = u.StudentId, 
                StudentIdXh = studentid.Xh,
                StudentIdName = studentid.Name,
                KechengId = u.KechengId, 
                KechengIdName = kechengid.Name,
                IsYuxuan = u.IsYuxuan,
                Chengji = u.Chengji,
                Remark = u.Remark,
                CreateUserName = u.CreateUserName,
                UpdateUserName = u.UpdateUserName,
                StartTime = xuanketaskid.StartTime,
                EndTime = xuanketaskid.EndTime,
                XuankeActive = DateTime.Now > xuanketaskid.StartTime && DateTime.Now < xuanketaskid.EndTime && xuanketaskid.Active == true && xuanketaskid.Status == "2",
            });
        return await query.ToPagedListAsync(input.Page, input.PageSize);
    }

    /// <summary>
    /// 更新校本选课
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Save")]
    public async Task Save(SaveMyXuankeInput input)
    {
        var xuankeId = input.XuankeId;
        var kechengId = input.KechengId;
        var kecheng = await _kechengRep.GetByIdAsync(kechengId);
        var xuanke = await _rep.GetByIdAsync(xuankeId);

        if (xuanke.KechengId == input.KechengId)
        {
            throw Oops.Oh("选课信息没变动");
        }
        if (kecheng.LvquRenshu >= kecheng.ZhaoshengRenshu)
        {
            throw Oops.Oh("选课失败，选课人数已达上限。");
        }
        
        if (kecheng.BanjiLimit != null && kecheng.BanjiLimit > 0)
        {
            var banjiCount = await _rep.AsQueryable()
               .Where(u => u.XuankeTaskId == xuanke.XuankeTaskId && u.JiaoxueBanId == xuanke.JiaoxueBanId && u.KechengId == input.KechengId)
               .CountAsync();
            if (banjiCount >= kecheng.BanjiLimit)
            {
                throw Oops.Oh($"选课失败，每班限报{kecheng.BanjiLimit}名，人数已满。");
            }
        }

        if(xuanke.KechengId !=0 &&xuanke.KechengId != kechengId)
        {
            var oldkecheng = await _kechengRep.GetByIdAsync(xuanke.KechengId);
            if (oldkecheng != null)
            {
                oldkecheng.LvquRenshu = await _rep.AsQueryable().Where(p => p.XuankeTaskId == xuanke.XuankeTaskId && p.KechengId == oldkecheng.Id).CountAsync() - 1;
                await _kechengRep.UpdateAsync(oldkecheng);
            }
        }
        xuanke.KechengId = kechengId;
        await _rep.UpdateAsync(xuanke);

        kecheng.LvquRenshu = await _rep.AsQueryable().Where(p => p.XuankeTaskId == xuanke.XuankeTaskId && p.KechengId == kecheng.Id).CountAsync(); ;
        await _kechengRep.UpdateAsync(kecheng);
    }

    /// <summary>
    /// 获取课程列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "List")]
    public async Task<List<KechengOutput>> List(DeleteXuanKeInput input)
    {
        var xuanke = await _rep.GetByIdAsync(input.Id);
        return await _rep.Context.Queryable<Kecheng>()
            .LeftJoin<Teacher>((kecheng, teacherid) => kecheng.TeacherId == teacherid.Id)
            .Where(kecheng => kecheng.XuankeTaskId == xuanke.XuankeTaskId)
            .Select((kecheng, teacherid) => new KechengOutput
            {
                Id = kecheng.Id,
                XuankeTaskId = kecheng.XuankeTaskId,
                Name = kecheng.Name,
                Description = kecheng.Description,
                TeacherId = kecheng.TeacherId,
                TeacherIdName = teacherid.Name,
                Address = kecheng.Address,
                ZhaoshengRenshu = kecheng.ZhaoshengRenshu,
                LvquRenshu = kecheng.LvquRenshu,
                Remark = kecheng.Remark,
            })
            .ToListAsync();
    }

    /// <summary>
    /// 分页查询校本选课
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "PageListByTeacherId")]
    public async Task<SqlSugarPagedList<XuanKeOutput>> PageListByTeacherId(XuanKeInput input)
    {
        var query = _rep.AsQueryable()
            .WhereIF(!string.IsNullOrWhiteSpace(input.SearchKey), u =>
                u.Chengji.Contains(input.SearchKey.Trim())
                || u.Remark.Contains(input.SearchKey.Trim())
            )
            .WhereIF(input.XuankeTaskId > 0, u => u.XuankeTaskId == input.XuankeTaskId)
            .WhereIF(input.BanjiId > 0, u => u.BanjiId == input.BanjiId)
            .WhereIF(input.JiaoxueBanId > 0, u => u.JiaoxueBanId == input.JiaoxueBanId)
            .WhereIF(input.StudentId > 0, u => u.StudentId == input.StudentId)
            .WhereIF(input.KechengId > 0, u => u.KechengId == input.KechengId)
            .WhereIF(!string.IsNullOrWhiteSpace(input.Chengji), u => u.Chengji.Contains(input.Chengji.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.Remark), u => u.Remark.Contains(input.Remark.Trim()))
            //处理外键和TreeSelector相关字段的连接
            .LeftJoin<XuankeTask>((u, xuanketaskid) => u.XuankeTaskId == xuanketaskid.Id)
            .LeftJoin<Banji>((u, xuanketaskid, banjiid) => u.BanjiId == banjiid.Id)
            .LeftJoin<JiaoxueBan>((u, xuanketaskid, banjiid, jiaoxuebanid) => u.JiaoxueBanId == jiaoxuebanid.Id)
            .LeftJoin<Student>((u, xuanketaskid, banjiid, jiaoxuebanid, studentid) => u.StudentId == studentid.Id)
            .LeftJoin<Kecheng>((u, xuanketaskid, banjiid, jiaoxuebanid, studentid, kechengid) => u.KechengId == kechengid.Id)
            .OrderBy((u, xuanketaskid, banjiid, jiaoxuebanid, studentid, kechengid) => studentid.Xh)
            .Select((u, xuanketaskid, banjiid, jiaoxuebanid, studentid, kechengid) => new XuanKeOutput
            {
                Id = u.Id,
                XuankeTaskId = u.XuankeTaskId,
                XuankeTaskIdName = xuanketaskid.Name,
                BanjiId = u.BanjiId,
                BanjiIdName = banjiid.Name,
                JiaoxueBanId = u.JiaoxueBanId,
                JiaoxueBanIdName = jiaoxuebanid.Name,
                StudentId = u.StudentId,
                StudentIdXh = studentid.Xh,
                StudentIdName = studentid.Name,
                KechengId = u.KechengId,
                KechengIdName = kechengid.Name,
                IsYuxuan = u.IsYuxuan,
                Chengji = u.Chengji,
                Chuqin1 = u.Chuqin1,
                Chuqin2 = u.Chuqin2,
                Chuqin3 = u.Chuqin3,
                Chuqin4 = u.Chuqin4,
                Chuqin5 = u.Chuqin5,
                Chuqin6 = u.Chuqin6,
                Chuqin7 = u.Chuqin7,
                Chuqin8 = u.Chuqin8,
                Chuqin9 = u.Chuqin9,
                Chuqin10 = u.Chuqin10,
                Chuqin11 = u.Chuqin11,
                Chuqin12 = u.Chuqin12,
                Chuqin13 = u.Chuqin13,
                Chuqin14 = u.Chuqin14,
                Chuqin15 = u.Chuqin15,
                Chuqin16 = u.Chuqin16,
                Chuqin17 = u.Chuqin17,
                Chuqin18 = u.Chuqin18,
                Chuqin19 = u.Chuqin19,
                Chuqin20 = u.Chuqin20,
                Remark = u.Remark,
                CreateUserName = u.CreateUserName,
                UpdateUserName = u.UpdateUserName,
            });
        return await query.ToPagedListAsync(input.Page, input.PageSize);
    }


    /// <summary>
    /// 更新校本选课出勤情况
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "SaveChuqin")]
    public async Task SaveChuqin(ChuqinInput input)
    {
        var access_token = (await _topSdkService.GetAccessTokenAsync()).access_token;

        foreach (var item in input.Data)
        {
            var entity = await _rep.GetFirstAsync(u => u.Id == item.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
            entity.Chengji = item.Chengji;
            entity.Chuqin1 = item.Chuqin1;
            entity.Chuqin2 = item.Chuqin2;
            entity.Chuqin3 = item.Chuqin3;
            entity.Chuqin4 = item.Chuqin4;
            entity.Chuqin5 = item.Chuqin5;
            entity.Chuqin6 = item.Chuqin6;
            entity.Chuqin7 = item.Chuqin7;
            entity.Chuqin8 = item.Chuqin8;
            entity.Chuqin9 = item.Chuqin9;
            entity.Chuqin10 = item.Chuqin10;
            entity.Chuqin11 = item.Chuqin11;
            entity.Chuqin12 = item.Chuqin12;
            entity.Chuqin13 = item.Chuqin13;
            entity.Chuqin14 = item.Chuqin14;
            entity.Chuqin15 = item.Chuqin15;
            entity.Chuqin16 = item.Chuqin16;
            entity.Chuqin17 = item.Chuqin17;
            entity.Chuqin18 = item.Chuqin18;
            entity.Chuqin19 = item.Chuqin19;
            entity.Chuqin20 = item.Chuqin20;
            await _rep.UpdateAsync(entity);
            switch (input.zhouci)
            {
                case "1":
                    if (item.Chuqin1 == "旷课")
                    {
                        var jiaoxueban = await _jiaoxueBanRep.GetByIdAsync(entity.JiaoxueBanId);
                        if (jiaoxueban.BzrId != null)
                        {
                            var teacher = await _teacherRep.GetByIdAsync(jiaoxueban.BzrId);
                            var student = await _rep.Context.Queryable<Student>()
                                .Where(u => u.Id == entity.StudentId)
                                .Select(u => new { u.Name, u.Xh })
                                .FirstAsync();
                            var kecheng = await _rep.Context.Queryable<Kecheng>()
                                .Where(u => u.Id == entity.KechengId)
                                .Select(u => new { u.Name, u.TeacherId })
                                .FirstAsync();
                            var kechengTeacher = await _teacherRep.GetByIdAsync(kecheng.TeacherId);
                            await _topSdkService.SendMessage(access_token, teacher.Tel, $"旷课通知：{jiaoxueban.Name}{student.Name}({student.Xh})同学，{kecheng.Name}旷课。{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")},{kechengTeacher.Name}");
                        }
                    }
                    break;
                case "2":
                    if (item.Chuqin2 == "旷课")
                    {
                        var jiaoxueban = await _jiaoxueBanRep.GetByIdAsync(entity.JiaoxueBanId);
                        if (jiaoxueban.BzrId != null)
                        {
                            var teacher = await _teacherRep.GetByIdAsync(jiaoxueban.BzrId);
                            var student = await _rep.Context.Queryable<Student>()
                                .Where(u => u.Id == entity.StudentId)
                                .Select(u => new { u.Name, u.Xh })
                                .FirstAsync();
                            var kecheng = await _rep.Context.Queryable<Kecheng>()
                                .Where(u => u.Id == entity.KechengId)
                                .Select(u => new { u.Name, u.TeacherId })
                                .FirstAsync();
                            var kechengTeacher = await _teacherRep.GetByIdAsync(kecheng.TeacherId);
                            await _topSdkService.SendMessage(access_token, teacher.Tel, $"旷课通知：{jiaoxueban.Name}{student.Name}({student.Xh})同学，{kecheng.Name}旷课。{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")},{kechengTeacher.Name}");
                        }
                    }
                    break;
                case "3":
                    if (item.Chuqin3 == "旷课")
                    {
                        var jiaoxueban = await _jiaoxueBanRep.GetByIdAsync(entity.JiaoxueBanId);
                        if (jiaoxueban.BzrId != null)
                        {
                            var teacher = await _teacherRep.GetByIdAsync(jiaoxueban.BzrId);
                            var student = await _rep.Context.Queryable<Student>()
                                .Where(u => u.Id == entity.StudentId)
                                .Select(u => new { u.Name, u.Xh })
                                .FirstAsync();
                            var kecheng = await _rep.Context.Queryable<Kecheng>()
                                .Where(u => u.Id == entity.KechengId)
                                .Select(u => new { u.Name, u.TeacherId })
                                .FirstAsync();
                            var kechengTeacher = await _teacherRep.GetByIdAsync(kecheng.TeacherId);
                            await _topSdkService.SendMessage(access_token, teacher.Tel, $"旷课通知：{jiaoxueban.Name}{student.Name}({student.Xh})同学，{kecheng.Name}旷课。{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")},{kechengTeacher.Name}");
                        }
                    }
                    break;
                case "4":
                    if (item.Chuqin4 == "旷课")
                    {
                        var jiaoxueban = await _jiaoxueBanRep.GetByIdAsync(entity.JiaoxueBanId);
                        if (jiaoxueban.BzrId != null)
                        {
                            var teacher = await _teacherRep.GetByIdAsync(jiaoxueban.BzrId);
                            var student = await _rep.Context.Queryable<Student>()
                                .Where(u => u.Id == entity.StudentId)
                                .Select(u => new { u.Name, u.Xh })
                                .FirstAsync();
                            var kecheng = await _rep.Context.Queryable<Kecheng>()
                                .Where(u => u.Id == entity.KechengId)
                                .Select(u => new { u.Name, u.TeacherId })
                                .FirstAsync();
                            var kechengTeacher = await _teacherRep.GetByIdAsync(kecheng.TeacherId);
                            await _topSdkService.SendMessage(access_token, teacher.Tel, $"旷课通知：{jiaoxueban.Name}{student.Name}({student.Xh})同学，{kecheng.Name}旷课。{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")},{kechengTeacher.Name}");
                        }
                    }
                    break;
                case "5":
                    if (item.Chuqin5 == "旷课")
                    {
                        var jiaoxueban = await _jiaoxueBanRep.GetByIdAsync(entity.JiaoxueBanId);
                        if (jiaoxueban.BzrId != null)
                        {
                            var teacher = await _teacherRep.GetByIdAsync(jiaoxueban.BzrId);
                            var student = await _rep.Context.Queryable<Student>()
                                .Where(u => u.Id == entity.StudentId)
                                .Select(u => new { u.Name, u.Xh })
                                .FirstAsync();
                            var kecheng = await _rep.Context.Queryable<Kecheng>()
                                .Where(u => u.Id == entity.KechengId)
                                .Select(u => new { u.Name, u.TeacherId })
                                .FirstAsync();
                            var kechengTeacher = await _teacherRep.GetByIdAsync(kecheng.TeacherId);
                            await _topSdkService.SendMessage(access_token, teacher.Tel, $"旷课通知：{jiaoxueban.Name}{student.Name}({student.Xh})同学，{kecheng.Name}旷课。{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")},{kechengTeacher.Name}");
                        }
                    }
                    break;
                case "6":
                    if (item.Chuqin6 == "旷课")
                    {
                        var jiaoxueban = await _jiaoxueBanRep.GetByIdAsync(entity.JiaoxueBanId);
                        if (jiaoxueban.BzrId != null)
                        {
                            var teacher = await _teacherRep.GetByIdAsync(jiaoxueban.BzrId);
                            var student = await _rep.Context.Queryable<Student>()
                                .Where(u => u.Id == entity.StudentId)
                                .Select(u => new { u.Name, u.Xh })
                                .FirstAsync();
                            var kecheng = await _rep.Context.Queryable<Kecheng>()
                                .Where(u => u.Id == entity.KechengId)
                                .Select(u => new { u.Name, u.TeacherId })
                                .FirstAsync();
                            var kechengTeacher = await _teacherRep.GetByIdAsync(kecheng.TeacherId);
                            await _topSdkService.SendMessage(access_token, teacher.Tel, $"旷课通知：{jiaoxueban.Name}{student.Name}({student.Xh})同学，{kecheng.Name}旷课。{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")},{kechengTeacher.Name}");
                        }
                    }
                    break;
                case "7":
                    if (item.Chuqin7 == "旷课")
                    {
                        var jiaoxueban = await _jiaoxueBanRep.GetByIdAsync(entity.JiaoxueBanId);
                        if (jiaoxueban.BzrId != null)
                        {
                            var teacher = await _teacherRep.GetByIdAsync(jiaoxueban.BzrId);
                            var student = await _rep.Context.Queryable<Student>()
                                .Where(u => u.Id == entity.StudentId)
                                .Select(u => new { u.Name, u.Xh })
                                .FirstAsync();
                            var kecheng = await _rep.Context.Queryable<Kecheng>()
                                .Where(u => u.Id == entity.KechengId)
                                .Select(u => new { u.Name, u.TeacherId })
                                .FirstAsync();
                            var kechengTeacher = await _teacherRep.GetByIdAsync(kecheng.TeacherId);
                            await _topSdkService.SendMessage(access_token, teacher.Tel, $"旷课通知：{jiaoxueban.Name}{student.Name}({student.Xh})同学，{kecheng.Name}旷课。{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")},{kechengTeacher.Name}");
                        }
                    }
                    break;
                case "8":
                    if (item.Chuqin8 == "旷课")
                    {
                        var jiaoxueban = await _jiaoxueBanRep.GetByIdAsync(entity.JiaoxueBanId);
                        if (jiaoxueban.BzrId != null)
                        {
                            var teacher = await _teacherRep.GetByIdAsync(jiaoxueban.BzrId);
                            var student = await _rep.Context.Queryable<Student>()
                                .Where(u => u.Id == entity.StudentId)
                                .Select(u => new { u.Name, u.Xh })
                                .FirstAsync();
                            var kecheng = await _rep.Context.Queryable<Kecheng>()
                                .Where(u => u.Id == entity.KechengId)
                                .Select(u => new { u.Name, u.TeacherId })
                                .FirstAsync();
                            var kechengTeacher = await _teacherRep.GetByIdAsync(kecheng.TeacherId);
                            await _topSdkService.SendMessage(access_token, teacher.Tel, $"旷课通知：{jiaoxueban.Name}{student.Name}({student.Xh})同学，{kecheng.Name}旷课。{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")},{kechengTeacher.Name}");
                        }
                    }
                    break;
                case "9":
                    if (item.Chuqin9 == "旷课")
                    {
                        var jiaoxueban = await _jiaoxueBanRep.GetByIdAsync(entity.JiaoxueBanId);
                        if (jiaoxueban.BzrId != null)
                        {
                            var teacher = await _teacherRep.GetByIdAsync(jiaoxueban.BzrId);
                            var student = await _rep.Context.Queryable<Student>()
                                .Where(u => u.Id == entity.StudentId)
                                .Select(u => new { u.Name, u.Xh })
                                .FirstAsync();
                            var kecheng = await _rep.Context.Queryable<Kecheng>()
                                .Where(u => u.Id == entity.KechengId)
                                .Select(u => new { u.Name, u.TeacherId })
                                .FirstAsync();
                            var kechengTeacher = await _teacherRep.GetByIdAsync(kecheng.TeacherId);
                            await _topSdkService.SendMessage(access_token, teacher.Tel, $"旷课通知：{jiaoxueban.Name}{student.Name}({student.Xh})同学，{kecheng.Name}旷课。{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")},{kechengTeacher.Name}");
                        }
                    }
                    break;
                case "10":
                    if (item.Chuqin10 == "旷课")
                    {
                        var jiaoxueban = await _jiaoxueBanRep.GetByIdAsync(entity.JiaoxueBanId);
                        if (jiaoxueban.BzrId != null)
                        {
                            var teacher = await _teacherRep.GetByIdAsync(jiaoxueban.BzrId);
                            var student = await _rep.Context.Queryable<Student>()
                                .Where(u => u.Id == entity.StudentId)
                                .Select(u => new { u.Name, u.Xh })
                                .FirstAsync();
                            var kecheng = await _rep.Context.Queryable<Kecheng>()
                                .Where(u => u.Id == entity.KechengId)
                                .Select(u => new { u.Name, u.TeacherId })
                                .FirstAsync();
                            var kechengTeacher = await _teacherRep.GetByIdAsync(kecheng.TeacherId);
                            await _topSdkService.SendMessage(access_token, teacher.Tel, $"旷课通知：{jiaoxueban.Name}{student.Name}({student.Xh})同学，{kecheng.Name}旷课。{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")},{kechengTeacher.Name}");
                        }
                    }
                    break;
                case "11":
                    if (item.Chuqin11 == "旷课")
                    {
                        var jiaoxueban = await _jiaoxueBanRep.GetByIdAsync(entity.JiaoxueBanId);
                        if (jiaoxueban.BzrId != null)
                        {
                            var teacher = await _teacherRep.GetByIdAsync(jiaoxueban.BzrId);
                            var student = await _rep.Context.Queryable<Student>()
                                .Where(u => u.Id == entity.StudentId)
                                .Select(u => new { u.Name, u.Xh })
                                .FirstAsync();
                            var kecheng = await _rep.Context.Queryable<Kecheng>()
                                .Where(u => u.Id == entity.KechengId)
                                .Select(u => new { u.Name, u.TeacherId })
                                .FirstAsync();
                            var kechengTeacher = await _teacherRep.GetByIdAsync(kecheng.TeacherId);
                            await _topSdkService.SendMessage(access_token, teacher.Tel, $"旷课通知：{jiaoxueban.Name}{student.Name}({student.Xh})同学，{kecheng.Name}旷课。{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")},{kechengTeacher.Name}");
                        }
                    }
                    break;
                case "12":
                    if (item.Chuqin12 == "旷课")
                    {
                        var jiaoxueban = await _jiaoxueBanRep.GetByIdAsync(entity.JiaoxueBanId);
                        if (jiaoxueban.BzrId != null)
                        {
                            var teacher = await _teacherRep.GetByIdAsync(jiaoxueban.BzrId);
                            var student = await _rep.Context.Queryable<Student>()
                                .Where(u => u.Id == entity.StudentId)
                                .Select(u => new { u.Name, u.Xh })
                                .FirstAsync();
                            var kecheng = await _rep.Context.Queryable<Kecheng>()
                                .Where(u => u.Id == entity.KechengId)
                                .Select(u => new { u.Name, u.TeacherId })
                                .FirstAsync();
                            var kechengTeacher = await _teacherRep.GetByIdAsync(kecheng.TeacherId);
                            await _topSdkService.SendMessage(access_token, teacher.Tel, $"旷课通知：{jiaoxueban.Name}{student.Name}({student.Xh})同学，{kecheng.Name}旷课。{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")},{kechengTeacher.Name}");
                        }
                    }
                    break;
                case "13":
                    if (item.Chuqin13 == "旷课")
                    {
                        var jiaoxueban = await _jiaoxueBanRep.GetByIdAsync(entity.JiaoxueBanId);
                        if (jiaoxueban.BzrId != null)
                        {
                            var teacher = await _teacherRep.GetByIdAsync(jiaoxueban.BzrId);
                            var student = await _rep.Context.Queryable<Student>()
                                .Where(u => u.Id == entity.StudentId)
                                .Select(u => new { u.Name, u.Xh })
                                .FirstAsync();
                            var kecheng = await _rep.Context.Queryable<Kecheng>()
                                .Where(u => u.Id == entity.KechengId)
                                .Select(u => new { u.Name, u.TeacherId })
                                .FirstAsync();
                            var kechengTeacher = await _teacherRep.GetByIdAsync(kecheng.TeacherId);
                            await _topSdkService.SendMessage(access_token, teacher.Tel, $"旷课通知：{jiaoxueban.Name}{student.Name}({student.Xh})同学，{kecheng.Name}旷课。{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")},{kechengTeacher.Name}");
                        }
                    }
                    break;
                case "14":
                    if (item.Chuqin14 == "旷课")
                    {
                        var jiaoxueban = await _jiaoxueBanRep.GetByIdAsync(entity.JiaoxueBanId);
                        if (jiaoxueban.BzrId != null)
                        {
                            var teacher = await _teacherRep.GetByIdAsync(jiaoxueban.BzrId);
                            var student = await _rep.Context.Queryable<Student>()
                                .Where(u => u.Id == entity.StudentId)
                                .Select(u => new { u.Name, u.Xh })
                                .FirstAsync();
                            var kecheng = await _rep.Context.Queryable<Kecheng>()
                                .Where(u => u.Id == entity.KechengId)
                                .Select(u => new { u.Name, u.TeacherId })
                                .FirstAsync();
                            var kechengTeacher = await _teacherRep.GetByIdAsync(kecheng.TeacherId);
                            await _topSdkService.SendMessage(access_token, teacher.Tel, $"旷课通知：{jiaoxueban.Name}{student.Name}({student.Xh})同学，{kecheng.Name}旷课。{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")},{kechengTeacher.Name}");
                        }
                    }
                    break;
                case "15":
                    if (item.Chuqin15 == "旷课")
                    {
                        var jiaoxueban = await _jiaoxueBanRep.GetByIdAsync(entity.JiaoxueBanId);
                        if (jiaoxueban.BzrId != null)
                        {
                            var teacher = await _teacherRep.GetByIdAsync(jiaoxueban.BzrId);
                            var student = await _rep.Context.Queryable<Student>()
                                .Where(u => u.Id == entity.StudentId)
                                .Select(u => new { u.Name, u.Xh })
                                .FirstAsync();
                            var kecheng = await _rep.Context.Queryable<Kecheng>()
                                .Where(u => u.Id == entity.KechengId)
                                .Select(u => new { u.Name, u.TeacherId })
                                .FirstAsync();
                            var kechengTeacher = await _teacherRep.GetByIdAsync(kecheng.TeacherId);
                            await _topSdkService.SendMessage(access_token, teacher.Tel, $"旷课通知：{jiaoxueban.Name}{student.Name}({student.Xh})同学，{kecheng.Name}旷课。{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")},{kechengTeacher.Name}");
                        }
                    }
                    break;
                case "16":
                    if (item.Chuqin16 == "旷课")
                    {
                        var jiaoxueban = await _jiaoxueBanRep.GetByIdAsync(entity.JiaoxueBanId);
                        if (jiaoxueban.BzrId != null)
                        {
                            var teacher = await _teacherRep.GetByIdAsync(jiaoxueban.BzrId);
                            var student = await _rep.Context.Queryable<Student>()
                                .Where(u => u.Id == entity.StudentId)
                                .Select(u => new { u.Name, u.Xh })
                                .FirstAsync();
                            var kecheng = await _rep.Context.Queryable<Kecheng>()
                                .Where(u => u.Id == entity.KechengId)
                                .Select(u => new { u.Name, u.TeacherId })
                                .FirstAsync();
                            var kechengTeacher = await _teacherRep.GetByIdAsync(kecheng.TeacherId);
                            await _topSdkService.SendMessage(access_token, teacher.Tel, $"旷课通知：{jiaoxueban.Name}{student.Name}({student.Xh})同学，{kecheng.Name}旷课。{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")},{kechengTeacher.Name}");
                        }
                    }
                    break;
                case "17":
                    if (item.Chuqin17 == "旷课")
                    {
                        var jiaoxueban = await _jiaoxueBanRep.GetByIdAsync(entity.JiaoxueBanId);
                        if (jiaoxueban.BzrId != null)
                        {
                            var teacher = await _teacherRep.GetByIdAsync(jiaoxueban.BzrId);
                            var student = await _rep.Context.Queryable<Student>()
                                .Where(u => u.Id == entity.StudentId)
                                .Select(u => new { u.Name, u.Xh })
                                .FirstAsync();
                            var kecheng = await _rep.Context.Queryable<Kecheng>()
                                .Where(u => u.Id == entity.KechengId)
                                .Select(u => new { u.Name, u.TeacherId })
                                .FirstAsync();
                            var kechengTeacher = await _teacherRep.GetByIdAsync(kecheng.TeacherId);
                            await _topSdkService.SendMessage(access_token, teacher.Tel, $"旷课通知：{jiaoxueban.Name}{student.Name}({student.Xh})同学，{kecheng.Name}旷课。{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")},{kechengTeacher.Name}");
                        }
                    }
                    break;
                case "18":
                    if (item.Chuqin18 == "旷课")
                    {
                        var jiaoxueban = await _jiaoxueBanRep.GetByIdAsync(entity.JiaoxueBanId);
                        if (jiaoxueban.BzrId != null)
                        {
                            var teacher = await _teacherRep.GetByIdAsync(jiaoxueban.BzrId);
                            var student = await _rep.Context.Queryable<Student>()
                                .Where(u => u.Id == entity.StudentId)
                                .Select(u => new { u.Name, u.Xh })
                                .FirstAsync();
                            var kecheng = await _rep.Context.Queryable<Kecheng>()
                                .Where(u => u.Id == entity.KechengId)
                                .Select(u => new { u.Name, u.TeacherId })
                                .FirstAsync();
                            var kechengTeacher = await _teacherRep.GetByIdAsync(kecheng.TeacherId);
                            await _topSdkService.SendMessage(access_token, teacher.Tel, $"旷课通知：{jiaoxueban.Name}{student.Name}({student.Xh})同学，{kecheng.Name}旷课。{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")},{kechengTeacher.Name}");
                        }
                    }
                    break;
                case "19":
                    if (item.Chuqin19 == "旷课")
                    {
                        var jiaoxueban = await _jiaoxueBanRep.GetByIdAsync(entity.JiaoxueBanId);
                        if (jiaoxueban.BzrId != null)
                        {
                            var teacher = await _teacherRep.GetByIdAsync(jiaoxueban.BzrId);
                            var student = await _rep.Context.Queryable<Student>()
                                .Where(u => u.Id == entity.StudentId)
                                .Select(u => new { u.Name, u.Xh })
                                .FirstAsync();
                            var kecheng = await _rep.Context.Queryable<Kecheng>()
                                .Where(u => u.Id == entity.KechengId)
                                .Select(u => new { u.Name, u.TeacherId })
                                .FirstAsync();
                            var kechengTeacher = await _teacherRep.GetByIdAsync(kecheng.TeacherId);
                            await _topSdkService.SendMessage(access_token, teacher.Tel, $"旷课通知：{jiaoxueban.Name}{student.Name}({student.Xh})同学，{kecheng.Name}旷课。{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")},{kechengTeacher.Name}");
                        }
                    }
                    break;
                case "20":
                    if (item.Chuqin20 == "旷课")
                    {
                        var jiaoxueban = await _jiaoxueBanRep.GetByIdAsync(entity.JiaoxueBanId);
                        if (jiaoxueban.BzrId != null)
                        {
                            var teacher = await _teacherRep.GetByIdAsync(jiaoxueban.BzrId);
                            var student = await _rep.Context.Queryable<Student>()
                                .Where(u => u.Id == entity.StudentId)
                                .Select(u => new { u.Name, u.Xh })
                                .FirstAsync();
                            var kecheng = await _rep.Context.Queryable<Kecheng>()
                                .Where(u => u.Id == entity.KechengId)
                                .Select(u => new { u.Name, u.TeacherId })
                                .FirstAsync();
                            var kechengTeacher = await _teacherRep.GetByIdAsync(kecheng.TeacherId);
                            await _topSdkService.SendMessage(access_token, teacher.Tel, $"旷课通知：{jiaoxueban.Name}{student.Name}({student.Xh})同学，{kecheng.Name}旷课。{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")},{kechengTeacher.Name}");
                        }
                    }
                    break;
            }


        }
    }
}

